const app = getApp()

Page({
  data: {
    userInfo: {
      nickname: '鲨鱼牙线',
      avatar: ''
    },
    orderCounts: {
      pending: 2,
      traveling: 1,
      reviewing: 3,
      cancelled: 0
    },
    isDistributor: false // 是否已注册分销商
  },
  
  onLoad() {
    this.loadUserInfo()
    this.loadOrderCounts()
    this.checkDistributorStatus()
  },
  
  onShow() {
    // 从个人信息页面返回时刷新用户信息
    this.loadUserInfo()
    this.loadOrderCounts()
    this.checkDistributorStatus()
  },
  
  checkDistributorStatus() {
    // 检查是否已注册分销商
    const distributorInfo = wx.getStorageSync('distributorInfo')
    this.setData({
      isDistributor: !!distributorInfo && distributorInfo.registered
    })
  },
  
  loadUserInfo() {
    // 优先从本地存储加载，然后从全局数据，最后使用默认值
    const storedUserInfo = wx.getStorageSync('userInfo')
    const globalUserInfo = app.globalData.userInfo
    const userInfo = storedUserInfo || globalUserInfo || this.data.userInfo
    
    // 同步到全局数据
    if (storedUserInfo && !app.globalData.userInfo) {
      app.globalData.userInfo = storedUserInfo
    }
    
    this.setData({ userInfo })
  },
  
  loadOrderCounts() {
    const orders = app.globalData.orders || []
    const counts = {
      pending: orders.filter(o => o.status === 'pending').length,
      traveling: orders.filter(o => o.status === 'traveling').length,
      reviewing: orders.filter(o => o.status === 'reviewing').length,
      cancelled: orders.filter(o => o.status === 'cancelled' || o.status === 'refunded').length
    }
    this.setData({ orderCounts: counts })
  },
  
  goToPersonalInfo() {
    wx.navigateTo({
      url: '/pages/personal-info/personal-info'
    })
  },
  
  goToContacts() {
    wx.navigateTo({
      url: '/pages/contacts/contacts'
    })
  },
  
  goToFavorites() {
    wx.navigateTo({
      url: '/pages/favorites/favorites'
    })
  },
  
  goToBrowsingHistory() {
    wx.navigateTo({
      url: '/pages/browsing-history/browsing-history'
    })
  },
  
  goToOrders(e) {
    const status = e.currentTarget.dataset.status
    // 订单列表是 tabBar 页面，需要使用 switchTab 跳转
    // 由于 switchTab 不支持传参，我们先将状态存储到全局数据中
    app.globalData.targetOrderStatus = status
    wx.switchTab({
      url: '/pages/order-list/order-list'
    })
  },
  
  goToAllOrders() {
    // 跳转到订单列表页面，显示全部订单
    // 订单列表是 tabBar 页面，需要使用 switchTab 跳转
    app.globalData.targetOrderStatus = 'all'
    wx.switchTab({
      url: '/pages/order-list/order-list'
    })
  },
  
  goToBargains() {
    wx.navigateTo({
      url: '/pages/my-bargains/my-bargains'
    })
  },
  
  goToGroupBuys() {
    wx.navigateTo({
      url: '/pages/my-group-buys/my-group-buys'
    })
  },
  
  goToFlashSales() {
    wx.navigateTo({
      url: '/pages/my-flash-sales/my-flash-sales'
    })
  },
  
  goToInvoices() {
    wx.navigateTo({
      url: '/pages/my-invoices/my-invoices'
    })
  },
  
  goToHelp() {
    wx.navigateTo({
      url: '/pages/help-center/help-center'
    })
  },

  goToConsultation() {
    wx.navigateTo({
      url: '/pages/consultation/consultation'
    })
  },
  
  // 跳转到分销商注册页面
  goToDistributorRegister() {
    wx.navigateTo({
      url: '/pages/distributor-register/distributor-register'
    })
  },
  
  // 跳转到我的钱包
  goToMyWallet() {
    wx.navigateTo({
      url: '/pages/my-wallet/my-wallet'
    })
  },

  // 跳转到关于智云旅游
  goToAbout() {
    wx.navigateTo({
      url: '/pages/about-platform/about-platform'
    })
  }
})
